/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ecom.beans.entity;

import ecom.exceptions.AccountEntityException;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 *
 * @author Mariam Soumaré
 */
@Entity
@Table(name = "accounts")
@NamedQueries({
    @NamedQuery(name = "Accounts.findAll", query = "SELECT a FROM Accounts a"),
    @NamedQuery(name = "Accounts.findById", query = "SELECT a FROM Accounts a WHERE a.id = :id"),
    @NamedQuery(name = "Accounts.findByIban", query = "SELECT a FROM Accounts a WHERE a.iban = :iban"),
    @NamedQuery(name = "Accounts.findByOwner", query = "SELECT a FROM Accounts a WHERE a.owner = :owner"),
    @NamedQuery(name = "Accounts.findByIdClient", query = "SELECT a FROM Accounts a WHERE a.clients.id = :idUser"),
    @NamedQuery(name = "Accounts.findByBalance", query = "SELECT a FROM Accounts a WHERE a.balance = :balance")})
public class Accounts implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Long id;
    @Basic(optional = false)
    @Column(name = "IBAN")
    private String iban;
    @Basic(optional = false)
    @Column(name = "OWNER")
    private String owner;
    @Basic(optional = false)
    @Column(name = "BALANCE")
    private double balance;
    @JoinColumn(name = "ID_CLIENT", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private Clients clients;

    public Accounts() {
    }

    public Accounts(Long id) {
        this.id = id;
    }

    public Accounts(Long id, String iban, String owner, double balance) {
        this.id = id;
        this.iban = iban;
        this.owner = owner;
        this.balance = balance;
    }

    public Accounts(String iban, String accountOwner, double balance, Clients user) {
        this.iban = iban;
        this.owner = accountOwner;
        this.balance = balance;
        this.clients=user;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getIban() {
        return iban;
    }

    public void setIban(String iban) {
        this.iban = iban;
    }

    public String getOwner() {
        return owner;
    }

    public void setOwner(String owner) {
        this.owner = owner;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public Clients getClients() {
        return clients;
    }

    public void setClients(Clients clients) {
        this.clients = clients;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Accounts)) {
            return false;
        }
        Accounts other = (Accounts) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ecom.beans.entity.Accounts[id=" + id + "]";
    }

     /**
     * dépose une somme d'argent sur le compte
     * @param amount
     */
    public void deposit(double amount) throws AccountEntityException {
        // TODO: deposit pas encore implementée
        if (amount > 0d) {
            this.balance += amount;
        } else {
            throw new AccountEntityException("Le montant à déposer ne peut pas être négatif");
        }
    }

    /**
     * retire une somme d'argent du compte
     * @param amount
     * @return le somme d'argent qui reste
     */
    public double withdraw(double amount) throws AccountEntityException {
        // TODO withdraw pas encore implementée

        if (amount <= this.balance) {
            this.balance -= amount;
        } else {
            throw new AccountEntityException("Vous disposez pas suffisament d'argent");
        }
        return this.balance;
    }

}
